package nu.validator.htmlparser.impl;

import nu.validator.htmlparser.common.TokenHandler;
import nu.validator.htmlparser.dom.DefaultTreeBuilder;
import nu.validator.htmlparser.impl.ElementName;
import nu.validator.htmlparser.impl.HtmlAttributes;
import nu.validator.htmlparser.impl.Tokenizer;
import nu.validator.htmlparser.impl.TreeBuilder;

import org.xml.sax.SAXException;

/**
 * @author <a href="mailto:tulkas.web@gmail.com">Tulkas</a>
 *
 */
public class TreeBuilderProxy implements TokenHandler {
	private DefaultTreeBuilder treeBuilder;

	public TreeBuilderProxy(DefaultTreeBuilder treeBuilder) {
		this.treeBuilder = treeBuilder;
	}

	/* (non-Javadoc)
	 * @see nu.validator.htmlparser.common.TokenHandler#cdataSectionAllowed()
	 */
	@Override
	public boolean cdataSectionAllowed() throws SAXException {
		return treeBuilder.cdataSectionAllowed();
	}

	/* (non-Javadoc)
	 * @see nu.validator.htmlparser.common.TokenHandler#characters(char[], int, int)
	 */
	@Override
	public void characters(char[] buf, int start, int length)
			throws SAXException {
		treeBuilder.characters(buf, start, length);
	}

	/* (non-Javadoc)
	 * @see nu.validator.htmlparser.common.TokenHandler#comment(char[], int, int)
	 */
	@Override
	public void comment(char[] buf, int start, int length) throws SAXException {
		treeBuilder.comment(buf, start, length);
	}

	/* (non-Javadoc)
	 * @see nu.validator.htmlparser.common.TokenHandler#doctype(java.lang.String, java.lang.String, java.lang.String, boolean)
	 */
	@Override
	public void doctype(String name, String publicIdentifier,
			String systemIdentifier, boolean forceQuirks) throws SAXException {
		treeBuilder.doctype(name, publicIdentifier, systemIdentifier,
				forceQuirks);
	}

	/* (non-Javadoc)
	 * @see nu.validator.htmlparser.common.TokenHandler#endTag(nu.validator.htmlparser.impl.ElementName)
	 */
	@Override
	public void endTag(ElementName eltName) throws SAXException {
		treeBuilder.endTag(eltName);
	}

	/* (non-Javadoc)
	 * @see nu.validator.htmlparser.common.TokenHandler#endTokenization()
	 */
	@Override
	public void endTokenization() throws SAXException {
		treeBuilder.endTokenization();
	}

	/* (non-Javadoc)
	 * @see nu.validator.htmlparser.common.TokenHandler#eof()
	 */
	@Override
	public void eof() throws SAXException {
		treeBuilder.eof();
	}

	/* (non-Javadoc)
	 * @see nu.validator.htmlparser.common.TokenHandler#startTag(nu.validator.htmlparser.impl.ElementName, nu.validator.htmlparser.impl.HtmlAttributes, boolean)
	 */
	@Override
	public void startTag(ElementName eltName, HtmlAttributes attributes,
			boolean selfClosing) throws SAXException {
		if (!treeBuilder.isDocDeclared()
				&& (TreeBuilder.HTML == eltName.getGroup()
						|| TreeBuilder.HEAD == eltName.getGroup() || TreeBuilder.BODY == eltName
						.getGroup())) {
			treeBuilder.setDocDeclared(true);
		}
		treeBuilder.startTag(eltName, attributes, selfClosing);
	}

	/* (non-Javadoc)
	 * @see nu.validator.htmlparser.common.TokenHandler#startTokenization(nu.validator.htmlparser.impl.Tokenizer)
	 */
	@Override
	public void startTokenization(Tokenizer self) throws SAXException {
		treeBuilder.startTokenization(self);
	}

	/* (non-Javadoc)
	 * @see nu.validator.htmlparser.common.TokenHandler#wantsComments()
	 */
	@Override
	public boolean wantsComments() throws SAXException {
		return treeBuilder.wantsComments();
	}

	/* (non-Javadoc)
	 * @see nu.validator.htmlparser.common.TokenHandler#zeroOriginatingReplacementCharacter()
	 */
	@Override
	public void zeroOriginatingReplacementCharacter() throws SAXException {
		treeBuilder.zeroOriginatingReplacementCharacter();
	}

}
